package service

import (
	nvo "ksd-social-api/modules/note/model/vo"
	"ksd-social-api/modules/special/model/vo"
	"ksd-social-api/utils"
	"strconv"
)

type SpecialBindService struct{}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id查询资源信息，不包含内容
 */
func (service *SpecialBindService) GetSpecialBinds(bindId uint64, systemId uint64) (*vo.SpecialBindVo, error) {
	// 1: 根据id查询数据层
	specialBinds, err := specialBindMapper.GetSpecialBinds(bindId, systemId)
	specialBindVo := vo.SpecialBindVo{}
	utils.CopyProperties(&specialBindVo, specialBinds)
	return &specialBindVo, err
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id查询资源信息，不包含内容
 */
func (service *SpecialBindService) FindAdminSpecialBinds(specialId uint64, userId uint64, systemId uint64, flag bool) (pvos []*vo.SpecialBindVo, err error) {
	// 1: 根据id查询数据层
	specialBinds, _ := specialBindMapper.FindAdminSpecialBinds(specialId, systemId)
	specialBindVos := []*vo.SpecialBindVo{}
	for _, specialBind := range specialBinds {
		specialBindVo := vo.SpecialBindVo{}
		utils.CopyProperties(&specialBindVo, specialBind)
		details, _ := service.FindAdminSpecialBindsDetail(specialId, specialBind.Id, userId, systemId, specialBind.Type, flag)
		if userId > 0 {
			// 查询用户是否购买分支
			specialBindVo.BuyFlag = specialBuyMapper.CountUserBindBuySpecialBind(specialBind.Id, specialId, userId, systemId)
		}
		// 绑定明细
		specialBindVo.Children = details
		// 转换vo返回
		specialBindVos = append(specialBindVos, &specialBindVo)
	}
	return specialBindVos, nil
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id查询资源信息，不包含内容
 */
func (service *SpecialBindService) FindAdminSpecialBindsDetail(specialId uint64, bindId uint64, userId uint64, systemId uint64,
	stype int, flag bool) (pvos []*vo.SpecialBindDetailVo, err error) {
	// 1: 根据id查询数据层
	specialBindDetails, _ := specialBindMapper.FindAdminSpecialBindDetails(specialId, bindId, systemId)
	specialBindDetailVos := []*vo.SpecialBindDetailVo{}
	for _, specialBindDetail := range specialBindDetails {
		specialBindDetailVo := vo.SpecialBindDetailVo{}
		utils.CopyProperties(&specialBindDetailVo, specialBindDetail)
		specialBindDetailVo.Id.Data = strconv.FormatUint(specialBindDetail.Id, 10)
		specialBindDetailVo.NoteId.Data = strconv.FormatUint(specialBindDetail.NoteId, 10)
		specialBindDetailVo.TeacherId.Data = strconv.FormatUint(specialBindDetail.TeacherId, 10)
		specialBindDetailVo.SpecialId.Data = strconv.FormatUint(specialBindDetail.SpecialId, 10)
		specialBindDetailVo.BindId.Data = strconv.FormatUint(specialBindDetail.BindId, 10)
		specialBindDetailVos = append(specialBindDetailVos, &specialBindDetailVo)
	}

	if len(specialBindDetailVos) > 0 {
		// 小册
		if stype == 1 {
			var noteIds []uint64
			for _, detailVo := range specialBindDetailVos {
				parseUint, _ := strconv.ParseUint(detailVo.NoteId.Data, 10, 64)
				noteIds = append(noteIds, parseUint)
			}
			specialBindDetailNewVos := []*vo.SpecialBindDetailVo{}
			noteList := noteMapper.FindNoteByIDS(noteIds)
			if len(noteList) > 0 {
				studyVos := []*nvo.NoteStudyVo{}
				if userId > 0 && flag {
					studyVos = noteMapper.CountNoteSpecialStudys(specialId, userId, systemId)
				}
				for _, detailVo := range specialBindDetailVos {
					for _, note := range noteList {
						if note.Id == detailVo.NoteId.Uint64() {
							specialBindDetailVo := vo.SpecialBindDetailVo{}
							utils.CopyProperties(&specialBindDetailVo, note)
							specialBindDetailVo.UpdateTime = note.UpdateTime
							specialBindDetailVo.Ntype = note.Ntype
							specialBindDetailVo.Userid.Data = strconv.FormatUint(note.Userid, 10)
							if userId > 0 && len(studyVos) > 0 && flag {
								for _, studyVo := range studyVos {
									if studyVo.NoteId == note.Id {
										specialBindDetailVo.Percent = studyVo.Percent
									}
								}
							}
							specialBindDetailVo.Id = detailVo.Id
							specialBindDetailVo.BindId = detailVo.BindId
							specialBindDetailVo.NoteId = detailVo.NoteId
							specialBindDetailVo.Sorted = detailVo.Sorted
							specialBindDetailVo.Status = detailVo.Status
							specialBindDetailNewVos = append(specialBindDetailNewVos, &specialBindDetailVo)

						}
					}
				}
			}
			return specialBindDetailNewVos, nil
		}

		// 老师
		if stype == 3 {
			var teacherIds []uint64
			for _, detailVo := range specialBindDetailVos {
				parseUint, _ := strconv.ParseUint(detailVo.TeacherId.Data, 10, 64)
				teacherIds = append(teacherIds, parseUint)
			}
			specialBindDetailNewVos := []*vo.SpecialBindDetailVo{}
			userList := userMapper.FindUserByIDS(teacherIds)
			for _, detailVo := range specialBindDetailVos {
				for _, user := range *userList {
					if user.Id == detailVo.TeacherId.Uint64() {
						specialBindDetailVo := vo.SpecialBindDetailVo{}
						utils.CopyProperties(&specialBindDetailVo, user)
						specialBindDetailVo.Id = detailVo.Id
						specialBindDetailVo.Userid = detailVo.TeacherId
						specialBindDetailVo.BindId = detailVo.BindId
						specialBindDetailVo.Sorted = detailVo.Sorted
						specialBindDetailVo.Status = detailVo.Status
						specialBindDetailVo.Nickname = user.Nickname
						specialBindDetailVo.Username = user.Username
						specialBindDetailVo.Description = user.Job
						specialBindDetailVo.Avatar = user.Avatar
						specialBindDetailVo.Uuid = user.Uuid
						specialBindDetailVo.TeacherIntro = user.Intro
						specialBindDetailNewVos = append(specialBindDetailNewVos, &specialBindDetailVo)
					}
				}
			}
			return specialBindDetailNewVos, nil
		}
	}

	return nil, nil
}
